<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>字符串转换</title>
</head>
<body>
<script>
    /*
      按要求把首字母转换为大写
      '-hello-world' =>  'HelloWorld'
      '-hello-world-hi' =>  'HelloWorldHi'
      '-hel-lo-world-hi' =>  'HelLoWorldHi'
    * */
    const str = '-hello-world';

    function firstLetter(str) {
        let splStr = str.split('-');
        for (let i = 1; i < splStr.length; i++) {
            //先把第一位转换为大写, 再和其后续的字母拼接并返回
            const element = splStr[i][0].toUpperCase() + splStr[i].substr(1, splStr[i].length);
            if (splStr[splStr.length - 1] === "") {
                splStr.pop()
                splStr[i] = element
            } else {
                splStr[i] = element
            }
        }
        return splStr.join("")
    }

    console.log(firstLetter(str))
</script>
</body>
</html>